<template>
  <div class="inventory-list">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>库存管理</span>
        </div>
      </template>

      <!-- 搜索区域 -->
      <div class="search-area">
        <el-form :model="searchForm" :inline="true" class="search-form">
          <el-form-item label="商品名称">
            <el-input
              v-model="searchForm.productName"
              placeholder="请输入商品名称"
              clearable
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="SKU编码">
            <el-input
              v-model="searchForm.skuCode"
              placeholder="请输入SKU编码"
              clearable
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="库存状态">
            <el-select
              v-model="searchForm.stockStatus"
              placeholder="请选择库存状态"
              clearable
              style="width: 150px"
            >
              <el-option label="正常" :value="1" />
              <el-option label="库存不足" :value="2" />
              <el-option label="缺货" :value="3" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">
              <el-icon><Search /></el-icon>
              搜索
            </el-button>
            <el-button @click="handleReset">
              <el-icon><Refresh /></el-icon>
              重置
            </el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 库存列表 -->
      <el-table
        :data="inventoryList"
        stripe
        border
        style="width: 100%"
      >
        <el-table-column prop="productName" label="商品名称" width="200" />
        <el-table-column prop="skuCode" label="SKU编码" width="150" />
        <el-table-column prop="skuName" label="SKU名称" width="150" />
        <el-table-column prop="currentStock" label="当前库存" width="100">
          <template #default="{ row }">
            <span :class="getStockClass(row.currentStock, row.lowStock)">
              {{ row.currentStock }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="lowStock" label="预警库存" width="100" />
        <el-table-column prop="stockStatus" label="库存状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStockStatusType(row.stockStatus)">
              {{ getStockStatusText(row.stockStatus) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="warehouse" label="仓库" width="120" />
        <el-table-column prop="updateTime" label="更新时间" width="180" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="handleAdjust(row)">
              调整库存
            </el-button>
            <el-button type="success" size="small" @click="handleView(row)">
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <div class="pagination-area">
        <el-pagination
          :current-page="pagination.pageNum"
          :page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { Search, Refresh } from '@element-plus/icons-vue'

// 库存数据接口
interface Inventory {
  id: number
  productName: string
  skuCode: string
  skuName: string
  currentStock: number
  lowStock: number
  stockStatus: number
  warehouse: string
  updateTime: string
}

// 搜索表单
const searchForm = reactive({
  productName: '',
  skuCode: '',
  stockStatus: undefined
})

// 分页信息
const pagination = reactive({
  pageNum: 1,
  pageSize: 10
})

const total = ref(0)

// 库存列表
const inventoryList = ref<Inventory[]>([
  {
    id: 1,
    productName: 'iPhone 15 Pro',
    skuCode: 'IP15P-256-BLK',
    skuName: '深空黑色 256GB',
    currentStock: 5,
    lowStock: 10,
    stockStatus: 2,
    warehouse: '主仓库',
    updateTime: '2024-01-01 10:30:00'
  },
  {
    id: 2,
    productName: 'MacBook Pro',
    skuCode: 'MBP-16-SIL',
    skuName: '银色 16GB',
    currentStock: 3,
    lowStock: 8,
    stockStatus: 2,
    warehouse: '主仓库',
    updateTime: '2024-01-01 11:15:00'
  },
  {
    id: 3,
    productName: 'AirPods Pro',
    skuCode: 'APP-WHT',
    skuName: '白色',
    currentStock: 2,
    lowStock: 15,
    stockStatus: 2,
    warehouse: '主仓库',
    updateTime: '2024-01-01 14:20:00'
  },
  {
    id: 4,
    productName: 'iPad Air',
    skuCode: 'IPA-64-BLU',
    skuName: '蓝色 64GB',
    currentStock: 25,
    lowStock: 20,
    stockStatus: 1,
    warehouse: '主仓库',
    updateTime: '2024-01-01 15:30:00'
  }
])

// 获取库存状态类型
const getStockStatusType = (status: number) => {
  const types = ['', 'success', 'warning', 'danger']
  return types[status] || 'info'
}

// 获取库存状态文本
const getStockStatusText = (status: number) => {
  const texts = ['', '正常', '库存不足', '缺货']
  return texts[status] || '未知'
}

// 获取库存数量样式
const getStockClass = (currentStock: number, lowStock: number) => {
  if (currentStock === 0) {
    return 'stock-danger'
  } else if (currentStock <= lowStock) {
    return 'stock-warning'
  } else {
    return 'stock-normal'
  }
}

// 搜索
const handleSearch = () => {
  pagination.pageNum = 1
  // 这里可以调用API搜索库存
}

// 重置搜索
const handleReset = () => {
  Object.assign(searchForm, {
    productName: '',
    skuCode: '',
    stockStatus: undefined
  })
  pagination.pageNum = 1
  // 这里可以调用API获取库存列表
}

// 调整库存
const handleAdjust = (inventory: Inventory) => {
  console.log('调整库存:', inventory)
}

// 查看详情
const handleView = (inventory: Inventory) => {
  console.log('查看详情:', inventory)
}

// 分页大小变化
const handleSizeChange = (size: number) => {
  pagination.pageSize = size
  pagination.pageNum = 1
  // 这里可以调用API获取库存列表
}

// 当前页变化
const handleCurrentChange = (page: number) => {
  pagination.pageNum = page
  // 这里可以调用API获取库存列表
}

// 初始化
onMounted(() => {
  total.value = inventoryList.value.length
})
</script>

<style scoped lang="scss">
.inventory-list {
  .card-header {
    font-weight: bold;
    color: #333;
  }

  .search-area {
    margin-bottom: 20px;
    padding: 20px;
    background-color: #f5f5f5;
    border-radius: 4px;
  }

  .pagination-area {
    margin-top: 20px;
    text-align: right;
  }

  .stock-normal {
    color: #67c23a;
    font-weight: bold;
  }

  .stock-warning {
    color: #e6a23c;
    font-weight: bold;
  }

  .stock-danger {
    color: #f56c6c;
    font-weight: bold;
  }
}
</style>

